package com.estebansm.apercuen.seguridad;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SeguridadFiltro implements Filter {
    private AutorizarBean autorizaBean = null; 
    
    public void init(FilterConfig filterConfig) throws ServletException {
  
    }

    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException,
                                                         ServletException {

      HttpServletRequest req = (HttpServletRequest) servletRequest;
      HttpSession session = req.getSession();
              
      if (session.getAttribute("AutorizarBean") == null){                                        
          this.autorizaBean = new AutorizarBean();
          session.setAttribute("AutorizarBean", autorizaBean);
          if (req.getRemoteUser() != null)
              this.autorizaBean.setUsuario(req.getRemoteUser().toLowerCase().trim()); 
      } else {
          this.autorizaBean = (AutorizarBean)session.getAttribute("AutorizarBean");  
      }
      
      filterChain.doFilter(servletRequest, servletResponse);
        
    }

    public void destroy() {
    }
}
